<?php

/**
 * @author DeineAgentur UG (haftungsbeschränkt) 
 * @www http://www.deineagentur.eu/
 * @copyright 2009
 */


class MOD_Register {
    
    private $smarty_data = array();
    private $error = array();
    private $error_msg = array();
    
    public function getUsername (){
        return "Dexus";
    }
	
    public function checkRegister () {
        
        $this->checkUsername($_POST['username']);
        $this->checkPassword($_POST['password'],$_POST['password2']);
        $this->checkMailInUse($_POST['email']);
        
    }
    
    public function checkMailInUse ($email) {
        global $mysqli;
        
        $sql =  $mysqli->stmt_init();
        $sql->prepare("SELECT `emailadresse` FROM `ds_user_data` WHERE `emailadresse` = ?");
        $sql->bind_param('s', $username);
        $sql->execute();
        $sql->store_result();
        $return = $sql->num_rows;
        $sql->close();
        
        if ($return == 0) {
            return true;
        } else {
            $this->error['email']=true;
            $this->error_msg['email']=_("Die angegebene eMail-Adresse wird bereits für einen anderen Account verwendet!");
            return false;
        }
        
    }
    
    /** Check Passwort: ist es itentisch? */
    public function Ajax_CheckUsername ($username) {
        
        $username = trim($username); // strip any white space
        $response = array(); // our response
        
        $result = $this->doCheckUsername($username);
        // if the username is blank
        if (! $result['ok']) {
            $response = $result;
            
        // this would live in an external library just to check if the username is taken
        } else if (!$this->CheckUsername($username)) {
            $response = array(
            'ok' => false, 
            'cssclass' => 'alert',
            'msg' => _('Der Benutername ist bereits vergeben.'));
            
        // it's all good
        } else {
            $response = array(
            'ok' => true, 
            'cssclass' => 'allok',
            'msg' => _('Der Benutzername ist verfügbar.'));
        }
        
        return $response;      
        
    }
    public function doCheckUsername ($username) {
        
        $username = trim($username); // strip any white space
        $response = array(); // our response
        
        // if the username is blank
        if (!$username) {
            $response = array(
            'ok' => false, 
            'cssclass' => 'alert',
            'msg' => _('Bitte gebe einen Benutzernamen an.'));
            
        // if the username does not match a-z or '.', '-', '_' then it's not valid
        } else if (strlen($username)<= 3) {
            $response = array(
            'ok' => false, 
            'cssclass' => 'alert',
            'msg' => _('Der Benutzername ist zu kurz.'));
            
        // this would live in an external library just to check if the username is taken
        } else if (strlen($username)> 40) {
            $response = array(
            'ok' => false, 
            'cssclass' => 'alert',
            'msg' => _('Der Benutzername ist zu lang.'));
            
        // this would live in an external library just to check if the username is taken
        } else if (!preg_match('/^[0-9A-Za-z.\-_]+$/', $username)) {
            $response = array(
            'ok' => false, 
            'cssclass' => 'alert',
            'msg' => _('Der Benutzername darf nur aus Buchstaben (A-Za-z), Zahlen (0-9) und den Sonderzeichen (.-_) bestehen.'));
            
        // this would live in an external library just to check if the username is taken
        }  else {
            $response = array(
            'ok' => true, 
            'cssclass' => 'allok',
            'msg' => _('Der Benutzername ist verfügbar.'));
        }
        
        return $response;
        
    }
    
    public function CheckUsername ($username) {
        global $mysqli;
        
        
        $sql =  $mysqli->stmt_init();
        if ($sql->prepare("SELECT `username` FROM `ds_user` WHERE `username` like ?")) {
        $sql->bind_param('s', $username);
        $sql->execute();
        $sql->store_result();
        $return = $sql->num_rows;
        $sql->close();
        } else {
            $this->error['username']=true;
            $this->error_msg['username']=_("Technischer fehler! Wenden Sie sich an den Betreiber!");
            return false;
        }
        
        $result = $this->doCheckUsername($username);
        // if the username is blank
        if (! $result['ok']) {
            $this->error['username']=true;
            $this->error_msg['username']=$result['msg'];
            return $result['ok'];
        }
        if ($return == 0) {
            return true;
        } else {
            $this->error['username']=true;
            $this->error_msg['username']=_("Der Benutzername wird schon verwendet!");
            return false;
        }
        
    }
    
    /** Check Passwort: ist es itentisch? */
    public function checkPassword ($passwd1,$passwd2) {
        
        if ($passwd1==$passwd2) {
            return true;
        } else {
            $this->error['passwd']=true;
            $this->error_msg['passwd']=_("Die Passwörter stimmen nicht überein!");
            return false;
        }
        
    }
    
    /** Smarty Output as Array **/
    public function smartyOutput () {
        $this->smarty_data['error'] = $this->error;
        $this->smarty_data['error_msg'] = $this->error_msg;
        return $this->smarty_data;
    }
    /** Smarty Set Data as Array for export (Smarty::assign) **/
    public function smarty_set($name,$value) {
        $this->smarty_data[$name] = $value;
    }
}

?>